package org.example.forum.service.impl;

import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.dao.BoardMapper;
import org.example.forum.exception.ApplicationException;

import org.example.forum.model.Board;
import org.example.forum.service.IBoradService;


import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BoradServiceImpl implements IBoradService {
    @Resource
    private BoardMapper boardMapper;

    public void create(Board board) {
        if(board==null){
          throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        board.setArticleCount(0);
        board.setState((byte) 0);
        board.setDeleteState((byte) 0);

        Date now = new Date();
        board.setCreateTime(now);
        board.setUpdateTime(now);
       int row= boardMapper.insertSelective(board);
       if(row!=1){
           throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
       }

        log.info("新增成功board= "+board.toString());
    }

    @Override
    public Board selectById(Long id) {
        if(id==null||id<=0){
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            throw new ApplicationException(AppResult
                    .failed(ResultCode.FAILED_NOT_EXISTS));
        }
         return board;
    }

    @Override
    public List<Board> selectByNum(Integer num) {
        if(num<1){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardMapper.selectTopByNum(num);
        //返回结果
        return boards;
    }
    public List<Board> selectAllNormal() {
        //层查询
        List<Board> boards = boardMapper.selectAllNormal();
        //返回结果
        return boards;
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        // 查询版块详情
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString() + ", board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        // 构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        // 判断减1之后是否小于0
        if (updateBoard.getArticleCount() < 0) {
            // 如果小于0那么设置为0
            updateBoard.setArticleCount(0);
        }

        // 调用DAO
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

}
